// The Parents scripts search and print the parents of a given nuclide.
// The nuclide can be submitted through the Command line arguments: Z or chemical symbol, A, Isomeric state number or letter
// or the nuclide can be edited at the end of the script, by default the script looks for U236 parents
// Name:            ParentsofNuclide
// Author:           r.d.
// last update:    03.03.2009

// For the given nuclide, the decay information of each parent is retrieved using the DecayParent(par)
// methode from the nuclide object. Using the parents material index from the decay info, the parent nuclide
// is created, in order to get its name which is printed in the result table together with the decay info.

// Each line of the result table is embeded in an html table row tag <tr> ... </tr> and
// each data field in the line is embeded in an html table data tag <td> ... </td>
// in order to get a well presented output on the print Preview tab.



int ParentNuclides(nuclide nuc)		// Get and print the parent nuclides of the nuclide nuc
//				//    return code: the number of parents nuclides
{
	decayInfo pdi;		// parent decay info
	nuclide parent;		// parent nuclide
	int par;			// parent counter
	int Z; int A; int L;
	for(par=0; par<nuc.DecayParents; par=par+1)	// all parent nuclides
	{
		pdi = nuc.DecayParent(par);		// get each parent decay info
		Z = pdi.Parent / 10000;
		A = pdi.Parent / 10 % 1000;
		L = pdi.Parent % 10;
		parent.Create(Z, A, L);		// create the parent nuclide to get the nuclide name
		string sQ = "&nbsp;";			// an non breakable space in html page
		if (pdi.Energy>=0) sQ = "" + pdi.Energy;	// or the Q Value if it is known
						// print a table header before the first parent
		if (par==0) print("<tr><td><b>Type of Decay of "+nuc.IsotopeScreenFormat+" parent nuclides</b></td><td><b>Branching Ratio</b></td><td><b>Decay Energy, Q[MeV]</b></td><td><b>Parents</b></td></tr>");
 						// print the basic decay informations for the parent
		print("<tr><td>"+pdi.DecayMode+"</td><td>"+pdi.BranchingRatio+"</td><td>" + sQ + "</td><td>"+parent.IsotopeScreenFormat+"</td></tr>");
	}
	return nuc.DecayParents;	// return the number of parent nuclides
}

int main(int argc, string argv[])
{
//	Use the command line arguments to submit a nuclide:
//		1. argument: the charge number Z or the chemical symbol e.g. "He" or 2 for helium
//		2. argument: the atomic mass number e.g. 4 for helium 4
//		3. argument: the isomeric state number 0,1,2 or 3 or the isomeric letter m, n or p; by default the ground state is assumed.
//     or	Edit the nuclide as you need:
//		1: the chemical symbol or Z-Number
//		2: the Atomic Mass Number
//		3: the isomeric state number, 0 for ground, 1 for m,...

	string Sy="U";int Z=-1; int A=236; int L=0;		// default nuclide: U-236

	string isomers = " mnp";
	if (argc>2)					// check the command line arguments
	{
		if (IsInt(argv[1]))		Z = ToInt(argv[1]);		// charge number Z
		else			Sy = argv[1];		// or chemical symbol
		if (IsInt(argv[2]))		A = ToInt(argv[2]);		// atomic mass number A
		if (argc>3)	if (IsInt(argv[3]))  	L = ToInt(argv[3]);		// isomeric state number
			else		L = isomers.IndexOf(argv[3]);	// or isemeric state letter
		else 			L = 0;			// or ground state by default
	}
	nuclide nuc;
	int rc=-1;
	if (Sy.CompareTo("")>0)	rc = nuc.Create(Sy, A, L);	// Create the desired Nuclide
	else         if (Z>=0)		rc = nuc.Create(Z, A, L);

	if (rc >= 0)		// and if it created successfully, that is if it exists,
	{
		print("Parent nuclides of " + nuc.IsotopeScreenFormat);	// print the name of the choosed nuclide
		ParentNuclides(nuc);		// search and print it parents
	}
	else
	{
		print("The nuclide " + Z + "  " + Sy + "  " + A + "  " + L + " cannot be created.");
		print("Check the command line arguments:");
		int i;
		for(i=0; i<argc; i=i+1)
			print("argv[" + i + "] = " + argv[i]);
	}
}